package com.hngy.xpq.shoppingcartmodule.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import androidx.annotation.Nullable;

import com.hngy.xpq.shoppingcartmodule.bean.Shop;
import com.hngy.xpq.shoppingcartmodule.database.entity.CartOrder;
import com.hngy.xpq.shoppingcartmodule.database.entity.ShopOrder;

import java.util.ArrayList;
import java.util.List;

public class SqlShop extends SQLiteOpenHelper {

    SQLiteDatabase sqLiteDatabase;

    /**
     * Create a helper object to create, open, and/or manage a database.
     * This method always returns very quickly.  The database is not actually
     * created or opened until one of {@link #getWritableDatabase} or
     * {@link #getReadableDatabase} is called.
     *
     * @param context to use for locating paths to the the database
     *                {@link #onUpgrade} will be used to upgrade the database; if the database is
     *                newer, {@link #onDowngrade} will be used to downgrade the database
     */
    public SqlShop(@Nullable Context context) {
        super(context, ShopOrder.DATABASE_DATA, null, ShopOrder.DATABASE_INS);
        sqLiteDatabase = this.getWritableDatabase();
    }

    /**
     * Called when the database is created for the first time. This is where the
     * creation of tables and the initial population of the tables should happen.
     *
     * @param db The database.
     */
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table " + ShopOrder.DATABASE_TABLE + "("
                + ShopOrder.NOTEPAD_ID + " integer primary key autoincrement,"
                + ShopOrder.NOTEPAD_Image + " VARCHAR(25),"
                + ShopOrder.NOTEPAD_NAME + " VARCHAR(50),"
                + ShopOrder.NOTEPAD_PRICE + " integer,"
                + ShopOrder.NOTEPAD_BUS + " VARCHAR(25),"
                + ShopOrder.NOTEPAD_DIS + " float,"
                + ShopOrder.NOTEPAD_TIME + " VARCHAR(25),"
                + ShopOrder.NOTEPAD_DES + " VARCHAR(25));");
    }

    /**
     * Called when the database needs to be upgraded. The implementation
     * should use this method to drop tables, add tables, or do anything else it
     * needs to upgrade to the new schema version.
     *
     * <p>
     * The SQLite ALTER TABLE documentation can be found
     * <a href="http://sqlite.org/lang_altertable.html">here</a>. If you add new columns
     * you can use ALTER TABLE to insert them into a live table. If you rename or remove columns
     * you can use ALTER TABLE to rename the old table, then create the new table and then
     * populate the new table with the contents of the old table.
     * </p><p>
     * This method executes within a transaction.  If an exception is thrown, all changes
     * will automatically be rolled back.
     * </p>
     *
     * @param db         The database.
     * @param oldVersion The old database version.
     * @param newVersion The new database version.
     */
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    /**
     * 增
     * @param s
     * @return
     */
    public boolean insert(Shop s) {
        ContentValues values = new ContentValues();
        values.put(ShopOrder.NOTEPAD_Image, s.getImage());
        values.put(ShopOrder.NOTEPAD_NAME, s.getName());
        values.put(ShopOrder.NOTEPAD_PRICE, s.getPrice());
        values.put(ShopOrder.NOTEPAD_BUS, s.getBus());
        values.put(ShopOrder.NOTEPAD_DIS, s.getDis());
        values.put(ShopOrder.NOTEPAD_TIME, s.getTime());
        values.put(ShopOrder.NOTEPAD_DES, s.getDes());
        return
                sqLiteDatabase.insert(ShopOrder.DATABASE_TABLE, null, values) > 0;
    }

    /**
     * 删
     * @param id
     * @return
     */
    public boolean delete(int id) {
        String sql = ShopOrder.NOTEPAD_ID + "=?";
        String[] va = new String[]{String.valueOf(id)};
        return
                sqLiteDatabase.delete(ShopOrder.DATABASE_TABLE, sql, va) > 0;
    }

    /**
     * 查
     * @return
     */
    public List<Shop> select() { // 全表遍历
        List<Shop> list = new ArrayList<Shop>();
        try {
            Cursor cursor = sqLiteDatabase.rawQuery("select * from " + ShopOrder.DATABASE_TABLE,
                    null, null);
            if (cursor != null) {
                while (cursor.moveToNext()) {
                    Shop s = new Shop();
                    String id = cursor.getString(cursor.getColumnIndex(ShopOrder.NOTEPAD_ID));
                    int ids = Integer.valueOf(id);
                    s.setId(ids);
                    s.setImage(cursor.getString(cursor.getColumnIndex(ShopOrder.NOTEPAD_Image)));
                    s.setName(cursor.getString(cursor.getColumnIndex(ShopOrder.NOTEPAD_NAME)));
                    s.setPrice(cursor.getInt(cursor.getColumnIndex(ShopOrder.NOTEPAD_PRICE)));
                    s.setBus(cursor.getString(cursor.getColumnIndex(ShopOrder.NOTEPAD_BUS)));
                    s.setDis(cursor.getFloat(cursor.getColumnIndex(ShopOrder.NOTEPAD_DIS)));
                    s.setTime(cursor.getString(cursor.getColumnIndex(ShopOrder.NOTEPAD_TIME)));
                    s.setDes(cursor.getString(cursor.getColumnIndex(ShopOrder.NOTEPAD_DES)));
                    list.add(s);
                }
                cursor.close();
            } else {
                Log.i("DataSql-select", "获取Data的信息出现问题!!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    public Shop select(int i) {
        Shop s = null;
        try {
            Cursor cursor = sqLiteDatabase.rawQuery("select * from " + ShopOrder.DATABASE_TABLE +
                            " where " + ShopOrder.NOTEPAD_ID + "=" + i,
                    null,null );
            if (cursor.moveToNext()){
                s = new Shop();
                String id = cursor.getString(cursor.getColumnIndex(ShopOrder.NOTEPAD_ID));
                int ids = Integer.valueOf(id);
                s.setId(ids);
                s.setImage(cursor.getString(cursor.getColumnIndex(ShopOrder.NOTEPAD_Image)));
                s.setName(cursor.getString(cursor.getColumnIndex(ShopOrder.NOTEPAD_NAME)));
                s.setPrice(cursor.getInt(cursor.getColumnIndex(ShopOrder.NOTEPAD_PRICE)));
                s.setBus(cursor.getString(cursor.getColumnIndex(ShopOrder.NOTEPAD_BUS)));
                s.setDis(cursor.getFloat(cursor.getColumnIndex(ShopOrder.NOTEPAD_DIS)));
                s.setTime(cursor.getString(cursor.getColumnIndex(ShopOrder.NOTEPAD_TIME)));
                s.setDes(cursor.getString(cursor.getColumnIndex(ShopOrder.NOTEPAD_DES)));
            } else {
                Log.i("DataSql-select", "连接异常!!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return s;
    }

}
